﻿#include "game.h"

void InitMS(char board[ROWS][COLS], int rows, int cols, char set)//初始化
{
	for (int i = 1; i < rows;i++)
	{
		for (int k = 1; k < cols; k++)
		{
			board[i][k] = set;
		}
	}
}

void PrintMS(char board[ROWS][COLS], int row, int col)//打印
{
	printf("------扫   雷------\n");
	printf("标");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ",i);
	}
	printf("\n");
	for (int i = 1; i <= row; i++)
	{
		printf("%d ",i);
		for (int k = 1; k <= col; k++)
		{
			printf("%c ", board[i][k]);
		}
		printf("\n");
	}
	printf("-------------------\n");
}


void MSTR(char board[ROWS][COLS], int row, int col)//布雷
{
	for (int i = 0; i < MSS; i++)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (x > 0 && x <= row && y>0 && y <= col)
		{
			board[x][y] = '1';
		}
	}
}

void MSPrintM(char board[ROWS][COLS], char boards[ROWS][COLS], int row, int col,int x,int y)//显示全部雷
{
	for (int i = 1; i <= row; i++)
	{
		for (int k = 0; k <= col; k++)
		{
			if (board[i][k] == '1')
			{
				boards[i][k] = '!';
			}
			else
			{
				boards[i][k] = ' ';
			}
		}
	}
	boards[x][y] = 'X';
}

int MSSTA(char board[ROWS][COLS], int x, int y)//计算雷数
{
	int sum = 0;
	for (int i = -1; i < 2; i++)
	{
		for (int k = -1; k < 2; k++)
		{
			if (board[x + i][y + k] == '1')
			{
				++sum;
			}
		}
	}
	return sum;
}



void MSchick(char board[ROWS][COLS], char boards[ROWS][COLS], int row, int col, int x, int y, int* ms)//自动展开
{
	if (x > 0 && x <= row && y > 0 && y <= col)
	{
		int sum = MSSTA(board, x, y);
		if (sum == 0)
		{
			if (boards[x][y] = '*')
			{
				--(*ms);
				boards[x][y] = ' ';
			}
			for (int i = -1; i < 2; i++)
			{
				for (int k = -1; k < 2; k++)
				{
					if (boards[x + i][y + k] == '*')
					{
						MSchick(board, boards, row, col, x+i, y+k, ms);
					}
				}
			}
		}
		else
		{
			--(*ms);
			boards[x][y] = '0'+sum;
		}
	}
}







void MSlabel(char boards[ROWS][COLS],int row, int col)//标记雷点
{
	while (1)
	{
	Order:
			int x = 0;
			int y = 0;
			printf("请输入标记坐标>");
			scanf("%d %d", &x, &y);
			if (x > 0 && x <= row && y > 0 && y <= col)
			{
				boards[x][y] = '?';
				break;
			}
			else
			{
				printf("坐标无效！请重新输入！\n");
				goto Order;
			}
	}
}

int MSWIN(char board[ROWS][COLS], char boards[ROWS][COLS], int row, int col)//检查正确标记了多少颗雷
{
	int sum = 0;
	for (int i = 1; i <= row; i++)
	{
		for (int k = 0; k <= col; k++)
		{
			if (board[i][k] == '1' && boards[i][k] == '?')
			{
				++sum;
			}
		}
	}
	return sum;
}

void MSWINS(char board[ROWS][COLS], char boards[ROWS][COLS], int row, int col)//胜利后输出所有雷爆炸效果
{
	for (int i = 0; i < row; i++)
	{
		for (int k = 0; k < col; k++)
		{
			if (board[i][k] == '1')
			{
				boards[i][k] = 'X';
			}
			else
			{
				boards[i][k] = ' ';
			}
		}
	}
}

void MSCAN(char board[ROWS][COLS], char boards[ROWS][COLS], int row, int col)//排雷
{
	int x = 0;
	int y = 0;
	int ms = ROW * COL - MSS;//总格数减去雷数就是无雷的区域
	while (ms)
	{
		system("cls");//清屏
		PrintMS(boards, row, col);//打印当前雷盘情况
		//PrintMS(board, ROW, COL);//作弊器！！！！！！！！！！！！！！
		printf("请输入排查坐标(0 0标记)>");
		scanf("%d %d", &x, &y);
		if(x==0&&y==0)//判断标记
		{
			MSlabel(boards, row, col);//标记
			system("cls");//清屏
			PrintMS(boards, row, col);//打印当前雷盘情况
		}
		else
		{
			if (x > 0 && x <= row && y > 0 && y <= col)//判断坐标合法性
			{
				if (boards[x][y] == '*' || boards[x][y] == '!')
				{
					if (board[x][y] == '1')
					{
						system("cls");//清屏
						printf("!!!很遗憾，您踩雷了，游戏结束!!!\n");
						MSPrintM(board, boards, row, col, x, y);//在显示盘上标记出全部雷
						PrintMS(boards, row, col);//打印雷盘
						printf("下次再接再厉哦！\n\n");
						system("pause");
						system("cls");
						break;
					}
					else
					{
						system("cls");//清屏
						PrintMS(boards, row, col);//打印当前雷盘情况
						MSchick(board, boards, row, col, x, y, &ms);//开始排查坐标--且自动排查直到有雷
						system("cls");//清屏
						PrintMS(boards, row, col);//打印当前雷盘情况
					}
				}
				else
				{
					printf("该坐标已被排查！\n");
					system("pause");
				}
			}
			else
			{
				printf("坐标错误！\n");
				system("pause");
			}
		}
	}

	if (ms == 0)//胜利后反馈数据
	{
		system("cls");//清屏
		int sum = MSWIN(board, boards, row, col);
		if (sum == MSS)
		{
			printf("恭喜你正确排完了所有的雷，你是当之无愧的排雷高手！\n");
		}
		else
		{
			if (sum != 0)
			{
				printf("恭喜你游戏胜利！你正确排除了%d颗雷\n",sum);
			}
			else
			{
				printf("恭喜你游戏胜利！\n");
			}
		}
		MSWINS(board, boards, row, col);
		PrintMS(boards, row, col);//打印雷盘
		system("pause");
		system("cls");//清屏
	}
}